package com.qf.common.core.interceptor;

import com.qf.common.core.annopation.NoLoginCheck;
import com.qf.common.core.constant.CommonConstant;
import com.qf.common.core.entity.User;
import com.qf.common.core.enums.RedisKeyEnum;
import com.qf.common.core.exception.CommonException;
import com.qf.common.core.utils.JwtUtils;
import com.qf.common.core.utils.SpringBeanUtils;
import com.qf.common.core.utils.UserUtils;
import com.qf.common.redis.utils.RedisUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * 2024/12/27
 */
public class LogInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 控制器的方法封装为HandlerMethod对象
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 获取方法名
            Method method = handlerMethod.getMethod();
            // 判断方法是否使用了NoLoginCheck注解
            boolean ret = method.isAnnotationPresent(NoLoginCheck.class);
            if (ret){
                return true;
            }
            String token = request.getHeader(CommonConstant.TOKEN);
                if (token == null || token.isEmpty()){
                    throw new CommonException("请重新登陆");
                }
                /*
                RedisUtil redisUtil = SpringBeanUtils.getBean(RedisUtil.class);
                User user = redisUtil.getCacheObject(String.format(RedisKeyEnum.KEY_LOGIN.getKey(), token));
                if (user == null){
                    throw new CommonException("请重新登陆");
                }*/
            try {
                Claims claims = JwtUtils.parseJWT(token);
                Integer uid = (Integer) claims.get("uid");
                // 将用户id写入到ThreadLocal中
                UserUtils.setUid(uid);
            }catch (Exception e){
                throw new CommonException("请重新登陆");
            }
        }
        return true;
    }

    // 控制层接口执行完成之后,从ThreadLocal中移除用户id(本质上是从当前线程的ThreadLocalMap对象中移除)
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserUtils.removeUid();
    }
    // @Override
    // public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //
    //     String token = request.getHeader("Token");
    //     if (token == null || token.isEmpty()){
    //         throw new CommonException("请重新登陆");
    //     }
    //     RedisUtil redisUtil = SpringBeanUtils.getBean(RedisUtil.class);
    //     User user = redisUtil.getCacheObject("token:" + token);
    //     if (user == null){
    //         throw new CommonException("请重新登陆");
    //     }
    //     return true;
    // }
}
